package net.flowas.genericcrud.audit.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.flowas.genericcrud.audit.ActionType;
import net.flowas.genericcrud.audit.AuditResource;
import net.flowas.genericcrud.audit.AuditService;
import net.flowas.genericcrud.audit.TreeWrapper;
import net.flowas.genericcrud.audit.model.AuditValue;
import net.flowas.genericcrud.audit.model.Comment;

public class AuditResourceImpl implements AuditResource{
    
	private AuditService auditService;
	
	public void setAuditService(AuditService auditService) {
		this.auditService = auditService;
	}

	@Override
	public void mark(String modelType, String action, Long id, Map<String,Object> data) {
		ActionType actionType=ActionType.valueOf(action);
		auditService.mark(modelType, actionType, id, data);
	}

	@Override
	public AuditValue get(String modelType, Long id) {
		return auditService.get(modelType,id);
	}

	@Override
	public List<TreeWrapper> findConments(String modelType, Long id) {
		List<Comment> list= auditService.findConments(modelType,id);
		List<TreeWrapper> plainTree = new ArrayList<>();
		for(Comment comment:list) {
			TreeWrapper<Comment> wrapper =new TreeWrapper<>();
			wrapper.setId(""+comment.getSerial());
			if(Comment.class.getSimpleName().equals(comment.getModelType())){
				wrapper.setParentId(""+comment.getAuditFor());
			}
			wrapper.setText(comment);
		}
		return TreeUtil.plainToTree(plainTree);
	}

}
